
#include <BFont>
#include <BStyle>
#include <BWidget>
#include <BMenuBar>

#include "IAlignment.h"
#include "member_BMenuBar.h"
#include "member_BMenu.h"

using namespace BWE;

member_BMenuBar::member_BMenuBar(BMenuBar* menuBar)
{
	boss = menuBar;
	orientation = Orientation_Horizontal;
	align = Align_Center;
	form = Form_None;
	spacing = 3;
	hoverIndex = -1;
	pressed = false;
	dirty = false;
}
member_BMenuBar::~member_BMenuBar()
{

}

void member_BMenuBar::freshRects()
{
	if (!dirty)
		return;

	const BStyle* style = boss->realStyle();

	const BFont* font = boss->font();
	if (!font)
		return;

	BRect crect = boss->centerRect();
	int ch = crect.height();
	int cw = crect.width();

	IAlignment alignment;
	alignment.ia_align = align;
	alignment.ia_form = form;

	contentSize.reset();

	int posX = crect.left() + spacing;
	int posY = crect.top();
	for (int i = 0; i < items.size(); i++)
	{
		MenuBarItem& item = items[i];
		if (item.dirty)
		{
			item.freshSize(style, font);
			item.dirty = false;
		}

		alignment.ia_imageSize = item.iconSize;
		alignment.ia_textSize = item.titleSize;
		alignment.adjust(BRect());

		item.rect.set(posX, posY, alignment.ia_hintSize.width(), ch);

		alignment.ia_spacing = spacing;
		alignment.adjust(item.rect);

		item.iconRect = alignment.ia_imageRect;
		item.textRect = alignment.ia_textRect;

		posX += item.rect.width() + spacing;
		contentSize.width() += item.rect.width() + spacing;
		if (contentSize.height() < alignment.ia_hintSize.height())
			contentSize.height() = alignment.ia_hintSize.height();
	}
	dirty = false;
}

void member_BMenuBar::slotMenuAbleChanged(BObject* object, const BValue& value)
{
	boss->fresh();
}
void member_BMenuBar::slotTitleChanged(BObject* object, const BValue& value)
{
	BMenu* menu = (BMenu*)object;
	for (int i = 0; i < items.size(); i++)
	{
		MenuBarItem& item = items[i];
		if (item.menu == menu)
		{
			item.dirty = true;
			dirty = true;
			boss->fresh();
			break;
		}
	}
}
void member_BMenuBar::slotTitleIconChanged(BObject* object, const BValue& value)
{
	BMenu* menu = (BMenu*)object;
	for (int i = 0; i < items.size(); i++)
	{
		MenuBarItem& item = items[i];
		if (item.menu == menu)
		{
			item.dirty = true;
			dirty = true;
			boss->fresh();
			break;
		}
	}
}
